####################################################################################################################
# Replication file for Treger:                                                                                     #
# Changing the lens: The contingency of results from conjoint experiments on the outcome variable and the estimand #
#                                                                                                                  #
# Reanalysis of: Jensen et al. (2021): City limits to partisan polarization in the American public. 
#
# Created by Clareta Treger                                                                                        #
# Last edited on 15 May 2025                                                                                       #
####################################################################################################################

#----------
#Cleaning House
rm(list=ls()) 
if(is.null(dev.list())==FALSE)dev.off() 
cat("\014") 
#----------


#loading required packages
library(psych)
library(stargazer)
library(foreign)
library(reshape2)
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
library(grid)
library(cowplot)
library(mlogit)
library(nnet)
library(readstata13)
library(tibble)
library(modelr)
library(haven)
library(stringr)
pacman::p_load(tidyverse, cregg, labelled, FindIt)
if("cjoint" %in% (.packages())){
  detach("package:cjoint", unload = TRUE)
}
pacman::p_load(srvyr, cregg, tidyverse, FindIt, estimatr, modelsummary)

###########################
###     Import Data     ###
###########################

# Note: use the dataset generated by the Stata code for reanlysis, 
# or the following data file: 

library(haven)
strong_rep_dem_conjoint <- read_dta
View(strong_rep_dem_conjoint)

###########################
###      Data Prep      ###
###########################

df <- strong_rep_dem_conjoint

df$educ_t <- NA
df$educ_t[df$educ==1] <- "Charter schools"
df$educ_t[df$educ==2] <- "Vouchers to schools" 
df$educ_t[df$educ==3] <- "Free pre-school"
df$educ_t[df$educ==4] <- "Pay teachers more"
df$educ_t[df$educ==5] <- "Keep current1" 
df$educ_t <- as.factor(df$educ_t)
df$educ_t <- relevel(df$educ_t, ref="Keep current1") #set "keep current" as the baselevel

df$hieduc_t <- NA
df$hieduc_t[df$hieduc==1] <- "Community colleges"
df$hieduc_t[df$hieduc==2] <- "Local public universities"
df$hieduc_t[df$hieduc==3] <- "Technical vocational training"
df$hieduc_t[df$hieduc==4] <- "Student grant programs"
df$hieduc_t[df$hieduc==5] <- "Keep current2" 
df$hieduc_t <- as.factor(df$hieduc_t)
df$hieduc_t <- relevel(df$hieduc_t, ref="Keep current2")

df$invest_t <- NA
df$invest_t[df$invest==1] <- "Attract new businesses"
df$invest_t[df$invest==2] <- "Stimulate existing companies"
df$invest_t[df$invest==3] <- "Encourage investment charities"
df$invest_t[df$invest==4] <- "Keep current3"
df$invest_t <- as.factor(df$invest_t)
df$invest_t <- relevel(df$invest_t, ref="Keep current3")

df$gov_t <- NA
df$gov_t[df$gov==1] <- "Consolidate local government"
df$gov_t[df$gov==2] <- "More power to the state"
df$gov_t[df$gov==3] <- "Keep current4"
df$gov_t <- as.factor(df$gov_t)
df$gov_t <- relevel(df$gov_t, ref="Keep current4")

df$workers_t <- NA
df$workers_t[df$workers==1] <- "Limit unions' power"
df$workers_t[df$workers==2] <- "Expand unions' power"
df$workers_t[df$workers==3] <- "Worker training vouchers"
df$workers_t[df$workers==4] <- "Tax breaks to entrepreneurs" 
df$workers_t[df$workers==5] <- "Keep current5" 
df$workers_t <- as.factor(df$workers_t)
df$workers_t <- relevel(df$workers_t, ref="Keep current5")

df$local_t <- NA
df$local_t[df$local==1] <- "Affordable housing"
df$local_t[df$local==2] <- "Public transportation"
df$local_t[df$local==3] <- "Safety and crime prevention"
df$local_t[df$local==4] <- "Keep current6"
df$local_t <- as.factor(df$local_t)
df$local_t <- relevel(df$local_t, ref="Keep current6")

df$strong_DR_t <- NA
df$strong_DR_t[df$strong_DR==0] <- "Strong Democrat"
df$strong_DR_t[df$strong_DR==1] <- "Strong Republican"
df$strong_DR_t <- as.factor(df$strong_DR_t)

df$plan_chosen1 <- as.factor(df$plan_chosen)

summary(df$invest_t)
summary(df$strong_DR_t)

str(df$strong_DR_t)
str(df$invest_t)
str(df$workers_t)
str(df$local_t)
str(df$gov_t)
str(df$educ_t)
str(df$hieduc_t)


###################################################
###      Analysis for Table and Figure D2       ###
###################################################

f1 <- plan_chosen ~ invest_t + workers_t +  local_t + gov_t + educ_t + hieduc_t

amce1 <- cj(df, f1,
            id = ~ caseid,
            estimate = "amce",
            by = ~ strong_DR_t,
            weights = ~weight )
plot(amce1, group = "strong_DR_t")
ggsave(filename = "amces_ct.pdf",
    width = 7,
  height = 7)

mm1 <- cj(df, f1,
            id = ~ caseid,
            estimate = "mm",
            by = ~ strong_DR_t,
            weights = ~weight )

plot(mm1, group = "strong_DR_t", vline = 0.5)
ggsave(filename = "mms_ct.pdf",
       width = 7,
       height = 7)

diff_mm1 <- cj(df, f1, id = ~ caseid, estimate = "mm_diff", 
               by = ~strong_DR_t, weights = ~weight)

#replace feature names
diff_mm1$feature <- str_replace(diff_mm1$feature, "invest_t", "Investment & Taxes")
diff_mm1$feature <- str_replace(diff_mm1$feature, "workers_t", "Workers & Entrepreneurs")
diff_mm1$feature <- str_replace(diff_mm1$feature, "local_t", "Local Services")
diff_mm1$feature <- str_replace(diff_mm1$feature, "gov_t", "Governance")
diff_mm1$feature <- str_replace(diff_mm1$feature, "educ_t", "Education")
diff_mm1$feature <- str_replace(diff_mm1$feature, "hieduc_t", "Higher Education")
diff_mm1$feature <- str_replace(diff_mm1$feature, "hiEducation", "Higher Education")

mm1$feature <- str_replace(mm1$feature, "invest_t", "Investment & Taxes")
mm1$feature <- str_replace(mm1$feature, "workers_t", "Workers & Entrepreneurs")
mm1$feature <- str_replace(mm1$feature, "local_t", "Local Services")
mm1$feature <- str_replace(mm1$feature, "gov_t", "Governance")
mm1$feature <- str_replace(mm1$feature, "educ_t", "Education")
mm1$feature <- str_replace(mm1$feature, "hieduc_t", "Higher Education")
mm1$feature <- str_replace(mm1$feature, "hiEducation", "Higher Education")

#change order of features in the plot
diff_mm1$feature <- factor(diff_mm1$feature, levels = c(
  "Investment & Taxes",
  "Workers & Entrepreneurs",
  "Local Services",
  "Governance",
  "Education",
  "Higher Education"
))

# Table D2
write.csv(diff_mm1, "mms diffs.csv")

# Figure D2
plot(diff_mm1, )
ggsave(filename = "mms diffs.pdf",
       width = 7,
       height = 7)

#### Extra ###
### plotting the mms and diffs together
plot(rbind(mm1, diff_mm1)) + ggplot2::facet_wrap(~BY, ncol = 3L)
ggsave(filename = "mms_and_diff.pdf",
       width = 7,
       height = 7)





